var Validator = new Class({
    Implements: Events,
    Implements: Options,
    form: null,
    pattern: null,
    options: {
        fields: {},
        patterns: {
            VALID_NOT_ZERO: '/[^0]/',
            VALID_NOT_EMPTY: '/.+/',
            VALID_NUMBER: '/^\d+$/',
            VALID_EMAIL: '/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/'
        },
        response: ''
    },
    initialize: function(el,options) {
        this.form = $(el);
        this.form.addEvent('submit',this.validate.bind(this)); 
        this.labels = this.form.getElements('label');
        this.setOptions(options);
    },
    validate: function() {
        for(i in this.options.fields) {
            var field = $(i);
            if(!this.checkField(field,this.options.fields[i])) {
                this.invalid(field,this.options.fields[i]);
                return false;
            } else {
                this.valid(field,this.options.fields[i]);
            };
        };        
    },
    checkField: function(field,obj){
        switch(field.nodeName){
            case 'INPUT':
                switch(field.type){
                    case 'file':
                    case 'hidden':
                    case 'password':
                    case 'text':
                        return this.typeText(field,obj);
                        break;
                    case 'checkbox':
                        return this.typeCheckbox(field,obj);
                        break;
                    case 'submit':
                    case 'button':
                        break;
                };
                break;
            case 'SELECT':
                return field.multiple ? this.typeMulti(field,obj) : this.typeSelect(field,obj);
                break;
            case 'TEXTAREA':
                return this.typeText(field,obj);
                break;
            case 'DIV':
                return this.typeRadio(field,obj);
                break;
        };
    },
    typeText: function(field,obj){
        var value = String($(field).value); 
        var pattern_key = obj.pattern; 
        if(typeof(pattern_key)==='string') {
            var pattern = this.options.patterns[pattern_key]; 
            this.pattern = pattern; 
            this.options.response = obj.response; 
            return value.search(eval(pattern))<0 ? false : true; 
        } else if(typeof(pattern_key)==='object') {
            var ret = false; 
            for(var i=0; i<pattern_key.length; i++) {
            var pattern = this.options.patterns[pattern_key[i]]; 
                ret = value.search(eval(pattern))<0 ? false : true; 
                if(!ret) {
                    this.options.pattern = pattern; 
                    this.options.response = obj.response[i]; 
                    this.pattern = pattern; 
                    return false;           
                }
            }
            return true; 
        }
    },
    typeCheckbox: function(field,obj){
        this.options.pattern = obj.pattern; 
        this.options.response = obj.response; 
        return field.checked; 
    },
    typeMulti: function(field,obj){
        this.options.pattern = obj.pattern; 
        this.options.response = obj.response; 
        return field.selectedIndex<0 ? false : true;
    },
    typeSelect: function(field,obj){
        this.options.pattern = obj.pattern; 
        this.options.response = obj.response; 
        return field.selectedIndex===0 ? false : true;
    },
    typeRadio: function(field,obj){
        var opts = $(field).getElements('input'); 
        var ret = false; 
        for(var i=0; i<opts.length; i++) {
            ret = opts[i].checked===true ? true : ret; 
        }
        this.options.pattern = obj.pattern; 
        this.options.response = obj.response; 
        return ret; 
    },
    valid: function(field,obj){
        field.removeClass('error');
        this.labels.each(function(item,index){
            item.removeClass('error');
            });
        return true;
    },
    invalid: function(field,obj){
        alert(this.options.response);
        field.addClass('error');
        this.labels.each(function(item,index){
                if(item.getProperty('for')==field.id) {
                    item.addClass('error');
                }
            });
        field.ref = this;
        if(this.selectable(field)) {
            field.select();
        }
        if(this.options.pattern===this.options.patterns.VALID_EMAIL) {
            field.value = '';
        }
        field.focus();
        return false;
    },
    selectable: function() {
        return false;
    }
}); 
